/**
 * @description 微博@ 关系 controller
 * @author malin
 */

const { getRelationCount, getAtUserBlogList, updateAtRelation } = require('../services/at-relation')
const { SuccessModel, ErrorModel } = require('../model/ResModel')
const { PAGE_SIZE } = require('../conf/constants')

/**
 * 获取@ 我的微博数量 
 * @param {number} userId 我的ID
 */
async function getAtMeCount(userId) {
    // service
    const count = await getRelationCount(userId)
    return new SuccessModel({
        count
    })
}

/**
 * 获取@ 用户的微博列表
 * @param {number} userId 我的ID
 * @param {number} pageIndex 
 */
async function getAtMeBlogList(userId, pageIndex = 0) {
    // service
    const result = await getAtUserBlogList({
        userId,
        pageIndex,
        pageSize: PAGE_SIZE
    })
    const { count, blogList } = result
    return new SuccessModel({
        isEmpty: blogList.length === 0,
        blogList,
        pageSize: PAGE_SIZE,
        pageIndex,
        count
    })
}

/**
 * 标记为已读
 * @param {number} userId 
 */
async function markAsRead(userId) {
    // service
    try {
        await updateAtRelation(
            { newIsRead: true },
            // 当满足userId，且未读时才更新
            { userId, isRead: false }
        )
    } catch(err) {
        console.error(err.message, err.stack)
    }
    // 不需要返回SuccessModel
}

module.exports = {
    getAtMeCount,
    getAtMeBlogList,
    markAsRead
}
